<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="DataList" row-key="id" :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="title">
        <template slot-scope="scope">
          {{scope.row.title}}
        </template>
      </el-table-column>
      <el-table-column label="链接地址" align="center" prop="url" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.url}}
        </template>
      </el-table-column>
      <el-table-column label="是否外部链接" align="center" prop="is_outside" :show-overflow-tooltip="true">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.sys_outside" :value="scope.row.is_outside"/>
        </template>
      </el-table-column>
      <el-table-column label="打开方式" align="center" prop="target" :show-overflow-tooltip="true">
      <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_target" :value="scope.row.target"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加开始 -->
    <el-dialog :title="title" :visible.sync="open"  append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="上级菜单" prop="pid">
          <treeselect
            v-model="form.pid"
            :options="MenuList"
            :normalizer="normalizer"
            :show-count="true"
            placeholder="选择上级菜单"
          />
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接地址" clearable size="small"/>
        </el-form-item>
        <el-form-item label="打开方式" prop="target">
          <el-select v-model="form.target" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_target"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否外部链接" prop="is_outside">
          <el-select v-model="form.is_outside" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_outside"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加结束 -->
  </div>
</template>

<script>
import * as Navigation from "@/api/operation/navigation";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import * as Auth from '@/utils/auth'

  export default {
    name: "Navigation",
    dicts: ['sys_target','sys_outside'],
    components: { Treeselect, IconSelect },
    data() {
      return {
        upload_url: process.env.VUE_APP_BASE_URL + "/file/upload",
        headers: Auth.getHeader(),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        open: false,
        form: {},
        rules: [],
        title: "",
        // 是否展开，默认全部折叠
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        // 日期范围
        dateRange: [],
        DataList: [],
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10
        },
        dialogVisible:false,
        dialogImageUrl:undefined,
        MenuList:[]
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询广告列表 */
      getList() {
        this.loading = true;
        Navigation.List(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
          this.DataList = this.handleTree(response.data, "id",'pid');
          this.loading = false;

          this.MenuList = [];
          const menu = { id: 0, title: '主类目', children: [] };
          menu.children = this.handleTree(response.data, "id",'pid');
          this.MenuList.push(menu);
        });
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.title,
          children: node.children
        };
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange=[];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加导航";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const Id = row.id || this.ids
        Navigation.Show(Id).then(response => {
          this.form = response.data;
          this.form.rewards=response.data.rewards;
          this.open = true;
          this.title = "修改导航";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              Navigation.Update(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              Navigation.Add(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const Id = row.id || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + Id + '"的数据项？').then(function() {
          return Navigation.Delete(Id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
  };
</script>
